/* 
 * File:   Boss.cpp
 * Author: paula
 * 
 * Created on 29 de noviembre de 2014, 16:26
 */

#include "Boss.h"

Boss::Boss() {
}

Boss::Boss(int x, int y, SDL_Renderer** gRenderer) {

    this->directionRight = true;
    this->jumping = false;
    this->attacking = false;
    this->attacked = false;
    this->walking = false;
    this->fallingDown = false;

    this->counterAttacked = 0;

    this->gRenderer = *gRenderer;
    this->dot = new Dot(x, y, 100, 100);
    this->path = "Img/Boss/E";
    this->state = WAITING;

    this->imageToDraw = this->boss0;

    this->loadMedia();
}

Boss::Boss(const Boss& orig) {
}

void Boss::changeImage(SBSImage* image) {

    SDL_Rect* principalSettings = new SDL_Rect();

    principalSettings->w = this->data.width;
    principalSettings->h = this->data.height;
    principalSettings->x = this->dot->getPos()->x;
    principalSettings->y = this->dot->getPos()->y;
    principalSettings->x = principalSettings->x - (principalSettings->w / 2);
    principalSettings->y = principalSettings->y - (principalSettings->h / 2);
    SDL_RendererFlip flip;
    if (directionRight) {
        flip = SDL_FLIP_NONE;
    } else {
        flip = SDL_FLIP_HORIZONTAL;
    }
    SDL_RenderCopyEx(gRenderer, image->getTexture(), NULL, principalSettings, 0, NULL, flip);
    delete principalSettings;
}

void Boss::loadMedia() {

    boss0 = new SBSImage();
    boss1 = new SBSImage();
    boss2 = new SBSImage();
    boss3 = new SBSImage();
    boss4 = new SBSImage();

    boss0->loadMedia(this->path + "/0.png", &gRenderer);
    boss1->loadMedia(this->path + "/1.png", &gRenderer);
    boss2->loadMedia(this->path + "/2.png", &gRenderer);
    boss3->loadMedia(this->path + "/3.png", &gRenderer);
    boss4->loadMedia(this->path + "/4.png", &gRenderer);

}

int Boss::getCharacterId() {

}

struct ObjectInit Boss::getData() {
    return this->data;
}

float Boss::getPosX() {
    return this->dot->getPos()->x;
}

float Boss::getPosY() {
    return this->dot->getPos()->y;
}

void Boss::updateData(struct ObjectInit data) {
    this->data = data;
}

void Boss::actualizePosition(float posX, float posY) {
    this->dot->setPos(posX, posY);
    this->dot->dotWorldToPixels();
}

void Boss::actualizeState(int state) {
    switch (state) {
        case JUMPING:
            this->setJumping();
            break;
        case ATTACKING:
            this->setAttacking();
            break;
        case ATTACKED:
            this->setAttacked();
            break;
        case FALLING:
            this->setFalling();
            break;
        default:
            this->setWaiting();
            break;
    }
    this->state = state;
}

void Boss::setWaiting() {
    this->jumping = false;
    this->attacking = false;
    this->fallingDown = false;
    this->waiting = true;
    this->counterAttacked = 0;
}

void Boss::setJumping() {
    this->jumping = true;
    this->attacking = false;
    this->attacked = false;
    this->fallingDown = false;
    this->jumping = false;
    this->waiting = false;
    this->counterAttacked = 0;
}

void Boss::setAttacked() {
    this->jumping = false;
    this->fallingDown = false;
    this->jumping = false;
    this->waiting = false;
    this->attacking = false;
    this->attacked = true;
}

void Boss::setAttacking() {
    this->jumping = false;
    this->fallingDown = false;
    this->jumping = false;
    this->waiting = false;
    this->attacking = true;
    this->attacked = false;
    this->counterAttacked = 0;
}

void Boss::setFalling() {
    this->jumping = false;
    this->attacking = false;
    this->jumping = false;
    this->waiting = false;
    this->fallingDown = true;
    this->attacked = false;
    this->counterAttacked = 0;
}

void Boss::actualize() {
    this->actualizePosition(this->data.x, this->data.y);
    this->actualizeState(this->data.stateCharacter);
    struct Event receivedEvent = this->data.event;
    this->directionRight = this->data.direction;
    switch (this->getState()) {
        case JUMPING:
            this->keepJumping();
            break;
        case ATTACKING:
            this->keepAttacking();
            break;
        case ATTACKED:
            this->keepAttacked();
            break;
        case FALLING:
            this->keepFallingDown();
            break;
        case WAITING:
            this->wait();
            break;
        default:
            this->wait();
            break;
    }
}

void Boss::keepJumping() {
    jumping = true;
    attacking = false;
    this->imageToDraw = boss1;
    jumping = false;
}

void Boss::keepAttacking() {
    this->imageToDraw = boss2;
}

void Boss::keepAttacked() {
    this->counterAttacked++;
    if (this->counterAttacked % 2 == 0) {
        this->imageToDraw = boss3;
    } else {
        this->imageToDraw = boss4;
    }
}

void Boss::keepFallingDown() {
    this->imageToDraw = boss1;
}

void Boss::wait() {
    this->walking = false;
    attacking = false;
    this->imageToDraw = boss0;
}

int Boss::getState() {
    return this->state;
}

void Boss::draw() {
    changeImage(this->imageToDraw);
}

void Boss::freeMedia() {

    delete boss0;
    delete boss1;
    delete boss2;
    delete boss3;
    delete boss4;

}

Boss::~Boss() {
    delete this->dot;
    this->freeMedia();
}

